package com.zhiqian.community.controller;

import com.zhiqian.community.entity.Page;
import com.zhiqian.community.service.ElasticsearchService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;
import java.util.Map;

@Controller
public class SearchController {

    @Autowired
    private ElasticsearchService elasticsearchService;

    @GetMapping("/search")
    public String search(String keyword, Page page, Model model){
        if (StringUtils.isBlank(keyword)){
            return "redirect:/index";
        }
        page.setPath("/search?keyword="+keyword);
        List<Map<String, Object>> result = elasticsearchService.search(keyword, page);
        model.addAttribute("postList",result);
        model.addAttribute("keyword",keyword);
        return "/site/search";
    }
}
